package com.hksj.common.quartz.service;

import com.hksj.common.quartz.exception.QuartzException;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class QuartzService {
    @Autowired
    private Scheduler scheduler;

    public void addJob(String jobName, String jobGroup, Class jobClazz , String cron, JobDataMap dataMap) throws QuartzException {
        try{
            JobKey jobKey = new JobKey(jobName,jobGroup);
            if(scheduler.checkExists(jobKey)) {
                throw new QuartzException("任务以存在");
            }

            JobBuilder jobBuilder = JobBuilder.newJob(jobClazz).withIdentity(jobKey);
            if (ObjectUtils.isEmpty(dataMap)){
                dataMap = new JobDataMap();
            }
            dataMap.put("cron",cron);
            jobBuilder.usingJobData(dataMap);
            JobDetail jobDetail = jobBuilder.build();

            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
            String triggerName = jobName + "-Trigger";
            String triggerGroup = jobName + "-TriggerGroup";

            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerName,triggerGroup)
                    .withSchedule(cronScheduleBuilder).build();
            scheduler.scheduleJob(jobDetail,trigger);
        }catch (QuartzException quartzException){
            throw new RuntimeException(quartzException.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("添加任务" + jobGroup + "-" + jobName +"失败");
        }
    }

    public void updateJob(String jobName, String jobGroup,Class jobClazz ,String cron,JobDataMap dataMap){
        try{
            removeJob(jobName,jobGroup);
            addJob(jobName,jobGroup,jobClazz,cron,dataMap);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("更新任务" + jobGroup + "-" + jobName +"失败");
        }
    }

    public void pauseJob(String jobName,String jobGroup) {
        try{
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            JobDetail jobDetail = scheduler.getJobDetail(jobKey);
            if (jobDetail == null) {
                return;
            }
            scheduler.pauseJob(jobKey);
        }catch (Exception e){
            throw new RuntimeException("暂停任务" + jobGroup + "-" + jobName +"失败");
        }
    }

    public void resumeJob(String jobName,String jobGroup) {
        try{
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            JobDetail jobDetail = scheduler.getJobDetail(jobKey);
            if (jobDetail == null) {
                return;
            }
            scheduler.resumeJob(jobKey);
        }catch (Exception e){
            throw new RuntimeException("恢复任务" + jobGroup + "-" + jobName +"失败");
        }
    }

    public void removeJob(String jobName, String jobGroup){
        try{
            String triggerName = jobName + "-Trigger";
            String triggerGroup = jobName + "-TriggerGroup";

            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
            Trigger trigger =  scheduler.getTrigger(triggerKey);
            if (trigger == null) {
                return;
            }
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            // 删除任务
            scheduler.deleteJob(jobKey);
        }catch (Exception e){
            throw new RuntimeException("删除任务" + jobGroup + "-" + jobName +"失败");
        }
    }
}
